package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;

import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;



import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;
    //根据手机号查询会员信息


    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量


    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if (months != null && months.size() > 0) {
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }

    @Override
    public void updateId(Member member) {
        memberDao.edit(member);
    }


    //会员档案分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //得到数据
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        //使用分页查询SQL语句拼接助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Member> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //会员编辑
    public void edit(Member member) {
        memberDao.edit(member);
    }

    @Override
    public List<Map<String,Object>> findByIds(Integer id) {
         List<Map<String,Object>> Array=new ArrayList<>();
            //查询所有的ID是这个的预约信息
            List<Order> list=orderDao.findCountById(id);
            //查询用户姓名
            Member member = memberDao.findById(id);
            //查询套餐名称
         for (Order order : list) {
             Map<String,Object> map=new HashMap<>();
             map.put("name",member.getName());
             Integer setmealId = order.getSetmealId();
             Setmeal setmeal=memberDao.findSetId(setmealId);
             map.put("id",order.getId());
             map.put("names",setmeal.getName());
             Array.add(map);
        }
        return Array;
    }

    @Override
    public List<Map<String, Object>> findByDate(Integer id, String startTime, String endTime) {
        List<Map<String,Object>> Array=new ArrayList<>();
        //查询所有的ID是这个的预约信息
        //加入新的时间条件
        List<Order> list=orderDao.findDate(id,startTime,endTime);
        //查询用户姓名
        Member member = memberDao.findById(id);
        //查询套餐名称
        for (Order order : list) {
            Map<String,Object> map=new HashMap<>();
            map.put("name",member.getName());
            Integer setmealId = order.getSetmealId();
            Setmeal setmeal=memberDao.findSetId(setmealId);
            map.put("names",setmeal.getName());
            map.put("id",order.getId());
            Array.add(map);
        }
        return Array;
    }

    @Override
    public Map<String, Object> findAll(Integer id) {
        return memberDao.findAlll(id);
    }

    @Override
    public Map<String, Object> getAll(String id) {
        return memberDao.getAll(id);
    }
    public Member findById1(Integer id) {
        return memberDao.findById(id);
    }

    //根据ID删除会员
    public void delete(Integer id) {
        //判断当前会员是否被引用
        long count = memberDao.findCountByMemberId(id);
        if (count > 0){
            System.out.println(count +"------------------------------------------------------");
            new RuntimeException();
        }
        memberDao.deleteById(id);
    }

    @Override
    public Map findById(Integer id) {

        return memberDao.findByIdAndOrderDate(id);
    }



}
